-- Radical by Basil Voronkov
-- Module 'inventory'
---------------------------------------------
-- This module implements an in game inventory with
-- a dedicated inventory screen.require "lib/core"
require "lib/drawing"

inventory = {

}

function inventory.init()
  _inventory = {};
  _activeitem = nil;
end

function inventory.count()
  return table.len(_inventory);
end

function inventory.add(o)
  if not inventory.has(o) then
    table.insert(_inventory, deref_if(o));
  end
end

function inventory.at(n)
  return ref(_inventory[n]);
end

function inventory.remove(o)
  local nm = deref_if(o);
  for i,v in ipairs(_inventory) do
    if v == nm then
      return table.remove(_inventory, i);
    end
  end
  inventory.clearitem();
end

function inventory.has(nm)
  if is_table(nm) then
    nm = deref(nm);
  end
  for i,v in ipairs(_inventory) do
    if v == nm then
      return true;
    end
  end
end

function inventory.clearitem()
  _activeitem = nil;
  theme.set("scr.gfx.cursor.normal", const.cursor);
end

function inventory.room(o)
  local ondrawbg = o.ondrawbg;
  local onclick = o.onclick;
  local ontimer = o.ontimer;
  o.ondrawbg    = function(s, p)
                    if ondrawbg ~= nil then
                      ondrawbg(s, p);
                    end
                    if s.nohud then
                      return p;
                    end
                    s.invmap = map(0, 0, const.picwidth, const.picheight);
                    local step = 66;
                    if inventory.count() > 7 then
                      step = 62;
                    end
                    local x,y = const.hudx,const.hudy;
                    if s.nav ~= nil then
                      y = y + step;
                    end
                    for i,v in ipairs(_inventory) do
                      local o = ref(v);
                      drawing.draw_pic(p, rcall(o.ico), x, y);
                      if _activeitem == v or (s.disableinv and not o.alwayson) then
                        local b = sprite.box(59, 60, "black", 150);--const.linkcolor);
                        sprite.draw(b, p, x-1, y-1);
                        sprite.free(b);
                      else
                        s.invmap:add(v, x, y, 57, 58);
                      end
                      y = y + step;
                    end
                    return p;
                  end
  o.ontimer     = function(s,x,y)
                    if ontimer ~= nil then
                      ontimer(s,x,y);
                    end
                    if s.invmap ~= nil then
                      local sel = s.invmap:get(x, y);
                      if s.olditem ~= nil and sel ~= s.olditem.sel then
                        drawing.clean(s.pic, const.hudx-1, s.olditem.y-1, 59, 60);
                        drawing.clean(s.pic, const.hudx-220, s.olditem.y+20, 220, 20);
                        s.olditem = nil;
                      end
                      if sel ~= nil and sel ~= _activeitem then
                        local b1 = sprite.box(59, 1, const.linkcolor);
                        local b2 = sprite.box(59, 1, const.linkcolor);
                        local b3 = sprite.box(1, 60, const.linkcolor);
                        local b4 = sprite.box(1, 60, const.linkcolor);
                        local y = s.invmap.items_[sel][2]-20;
                        
                        sprite.copy(b1, s.pic, const.hudx-1, y-1);
                        sprite.copy(b2, s.pic, const.hudx-1, y+58);
                        sprite.copy(b3, s.pic, const.hudx-1, y-1);
                        sprite.copy(b4, s.pic, const.hudx+57, y-1);
                        
                        sprite.free(b1);
                        sprite.free(b2);
                        sprite.free(b3);
                        sprite.free(b4);
                        
                        --[[
                        local b1,b2,y = sprite.box(59, 60, const.linkcolor),sprite.box(57, 58, "black"),s.invmap.items_[sel][2]-20;
                        sprite.copy(b1, s.pic, const.hudx-1, y-1);
                        sprite.free(b1);
                        sprite.copy(b2, s.pic, const.hudx, y);
                        sprite.free(b2);]]
                        
                        drawing.copy_pic(s.pic, rcall(ref(sel).ico), const.hudx, y);
                        drawing.textframe(s.pic, rcall(ref(sel).nam), const.hudx-10, y+20, 4, 2, true);
                        s.olditem = {sel=sel,y=y};
                      end
                    end
                  end
  o.onclick     = function(s,x,y)
                    if onclick ~= nil then
                      onclick(s,x,y);
                    end
                    if s.invmap ~= nil then
                      local sel = s.invmap:get(x, y);
                      if sel ~= nil then
                        if _activeitem ~= nil then
                          here().tmpdsc = ref(_activeitem):use(sel);
                          inventory.clearitem();
                          refresh();
                        else
                          ref(sel):activate();
                        end
                      end
                    end
                  end
  return o;
end